#!/usr/bin/perl

# $Id: database_builder.pl 262 2010-07-18 13:29:17Z dolbanavt $
# $Date: 2010-07-18 16:29:38 +0300 (Вск, 18 Июл 2010) $
# $HeadURL: https://robosearch.googlecode.com/svn/branches/reborn/database_builder.pl $
# $LastChangedRevision$

use lib::abs qw( base_lib lib );
use JIP::ToolSet;
use Carp qw( croak );
use Const::Fast qw( const );
use English qw( -no_match_vars );
use Pod::Usage qw( pod2usage );
use Getopt::Long 2.24 qw( :config no_auto_abbrev no_ignore_case );
use File::Spec::Functions qw( catfile );
use FindBin qw( $RealBin );
use JIP::Utils qw( get_log_name );
use DBWrapperFactory;
use BuildStepFactory;
use Configurator;
use SQLWrapper;
use Proc::Lock;
use Log;

our $VERSION = '1.0';

# more meaningfull names for pod2usage's -verbose parameter
const my $SHOW_USAGE_ONLY         => 0;
const my $SHOW_BRIEF_HELP_MESSAGE => 1;
const my $SHOW_FULL_MANUAL        => 2;

# more meaningfull names for pod2usage's -exitval parameter
const my $RETURN_SUCCESS     => 0;
const my $RETURN_FATAL_ERROR => 1;
const my $RETURN_BAD_OPTIONS => 2;

#-- ui -------------------------------------------------------------------------
my $options_okay = GetOptions(
    'help'   => \my $is_help_requested,
    'conf=s' => \my $path_to_config_file,
);

# show appropriate help message
pod2usage( -exitval => $RETURN_BAD_OPTIONS, -verbose => $SHOW_USAGE_ONLY )
    unless $options_okay;

pod2usage( -exitval => $RETURN_SUCCESS, -verbose => $SHOW_FULL_MANUAL )
    if $is_help_requested;

# check required options
pod2usage(
    -exitval => $RETURN_BAD_OPTIONS,
    -message => 'required parameter "conf" not defined',
    -verbose => $SHOW_USAGE_ONLY,
) unless $path_to_config_file;

#-- init settings --------------------------------------------------------------
my $conf = Configurator->new( conf => $path_to_config_file )->get_settings;

#-- init log -------------------------------------------------------------------
our $log = Log->new(
    log_file  => catfile( $conf->database_builder->log_dir, get_log_name() ),
    log_level => $conf->database_builder->log_level,
);

#-- signal handlers ------------------------------------------------------------
require( catfile( $RealBin, 'signal_handlers.pl' ) );

#-- setup lock -----------------------------------------------------------------
$log->Note('setup lock');

my $process = Proc::Lock->new(
    lock_file => $conf->database_builder->lock_file,
);

$process->lock();

#-- db wrapper -----------------------------------------------------------------
my $db_wrapper = DBWrapperFactory->create_builder_wrapper(
    dbh => SQLWrapper->new(
        dsn  => $conf->db->dsn,
        user => $conf->db->user,
        pass => $conf->db->pass,
    )->get_db_handle,
);

#-- main -----------------------------------------------------------------------
my @build_steps = qw( SetAgeing LoadNewContent );

for my $each_step ( @build_steps ) {
    my @step_params = (
        $each_step,
        {
            conf => $conf,
            log  => $log,
            dbh  => $db_wrapper,
        },
    );

    BuildStepFactory->create( @step_params )->execute();
}

#-- finalyze -------------------------------------------------------------------
$log->Note('-- finalyze -----------------------------------------------------');
$log->Note('unset lock...') and $process->unlock() and $log->Note('exiting');

#-- end ------------------------------------------------------------------------

__END__

=head1 NAME

database_builder.pl

=head1 SYNOPSIS

database_builder.pl [options]

 Options:
   --help            show full help message
   --conf            path to config file

=head1 USAGE

/path/to/database_builder.pl --help

/path/to/database_builder.pl --conf=./robo_conf.pm

=head1 DESCRIPTION

Удаляет из базы данных устаревшие данные и добавляет новые.

=head1 DEPENDENCIES

lib::abs (from CPAN)

Const::Fast (from CPAN)

JIP::ToolSet (from base_lib)

JIP::Utils (from base_lib)

DBWrapperFactory (from base_lib)

BuildStepFactory (from base_lib)

Configurator (from base_lib)

SQLWrapper (from base_lib)

Proc::Lock (from base_lib)

Log (from base_lib)

=head1 EXIT STATUS

0 - программа выполнена успешно

1 - критическая ошибка программы

2 - неверные параметры командной строки

=head1 AUTHOR

Vladimir Zhavoronkov, <dolbanavt (at) Google Mail>

http://dolbanavt.livejournal.com/

=head1 LICENSE AND COPYRIGHT

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=cut
